package com.have.blog.admin.service.impl;

import com.have.blog.admin.dao.AdminWebVisitDao;
import com.have.blog.admin.redis.WebVisitCacheService;
import com.have.blog.admin.service.AdminWebVisitService;
import com.have.blog.base.constant.BaseConstant;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.mapper.WebVisitMapper;
import com.have.blog.model.models.WebVisitExample;
import com.have.blog.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@SuppressWarnings("all")
public class AdminWebVisitServiceImpl implements AdminWebVisitService {
    @Autowired
    private WebVisitMapper webVisitMapper;
    @Autowired
    private AdminWebVisitDao adminWebVisitDao;
    @Autowired
    private WebVisitCacheService webVisitCacheService;

    @Override
    public Long getWebVisitCount() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = DateUtils.getToDayStartTime();
        String endTime = DateUtils.getToDayEndTime();
        WebVisitExample example = new WebVisitExample();
        WebVisitExample.Criteria criteria = example.createCriteria();
        try {
            criteria.andCreateTimeBetween(sdf.parse(startTime), sdf.parse(endTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return webVisitMapper.countByExample(example);
    }

    @Override
    public Wrapper<Map<String, Object>> getVisitByWeek() {
        List<Long> uvByWeek = webVisitCacheService.getUvByWeek();
        List<Long> pvByWeek = webVisitCacheService.getPvByWeek();

        Map<String, Object> resultMap = new HashMap<>(BaseConstant.NUM_THREE);
        ArrayList<String> resultSevenDayList = DateUtils.getDaysByN(7, "MM-dd");
        resultMap.put("date", resultSevenDayList);
        resultMap.put("pv", pvByWeek);
        resultMap.put("uv", uvByWeek);
        return Wrapper.success(resultMap);
    }
}
